C#

推荐列表 站点导航

当前位置:首页 > 脚本编程 > C# >

C#_C#实现的json序列化和反序列化代码实例,using System;using System.Collectio

来源:互联网  作者:网友投稿  发布时间:2021-01-07 08:30
C#实现的json序列化和反序列化代码实例,using System;using System.Collections.Generic;using System.Web.Script.Serialization;using System.Co...

[DataContract] public class Person1 {[IgnoreDataMember]public int Id { get; set; }[DataMember(Name = "name")]public string Name { get; set; }[DataMember(Name = "sex")]public string Sex { get; set; }}public partial class _Default : System.Web.UI.Page {string constr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;protected void Page_Load(object sender, p11);szJson = Encoding.UTF8.GetString(stream.ToArray());Response.Write(szJson);}//反序列化//using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(szJson)))//{// DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(People));// Person1 _people = (Person1)serializer.ReadObject(ms);//}}protected void Button1_Click(object sender, EventArgs e){Person p1 = new Person();p1.Id = 1;p1.Name = "dxw";Person p2 = new Person();p2.Id = 2;p2.Name = "wn";ListPerson listperson = new ListPerson();listperson.Add(p1);listperson.Add(p2);JavaScriptSerializer js = new JavaScriptSerializer();//json序列化string s = js.Serialize(listperson);Response.Write(s);//方法二Person1 p11 = new Person1();p11.Id = 1;p11.Name = "hello";p11.Sex = "男";DataContractJsonSerializer json = new DataContractJsonSerializer(p11.GetType());string szJson = "";//序列化using (MemoryStream stream = new MemoryStream()){json.WriteObject(stream,使用DataMember属性标记字段必须使用DataContract标记类 否则DataMember标记不起作用,DataMember:定义序列化属性参数, using System;using System.Collections.Generic;using System.Web.Script.Serialization;using System.Configuration;using System.Runtime.Serialization.Json;using System.Runtime.Serialization;using System.IO;using System.Text;namespace WebApplication1{//方法一:引入System.Web.Script.Serialization命名空间使用 JavaScriptSerializer类实现简单的序列化 [Serializable] public class Person {private int id;/// summary/// id/// /summarypublic int Id{get { return id; }set { id = value; }}private string name;/// summary/// 姓名/// /summarypublic string Name{get { return name; }set { name = value; }} }//方法二:引入 System.Runtime.Serialization.Json命名空间使用 DataContractJsonSerializer类实现序列化 //可以使用IgnoreDataMember:指定该成员不是数据协定的一部分且没有进行序列化。

EventArgs e){Response.Write(constr);}} ,。

相关热词:

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!

本文地址: https://v30.fanwenzhu.com/jiaob/c/11743.shtml

下一篇:没有了
最新文章
热门文章
Copyright © www.juheyunku.com      关于 | 合作 | 声明 | 联系 | 更新 | 地图 | Tags

C#_C#实现的json序列化和反序列化代码实例,using System;using System.Collectio

2021-01-07 编辑:网友投稿

[DataContract] public class Person1 {[IgnoreDataMember]public int Id { get; set; }[DataMember(Name = "name")]public string Name { get; set; }[DataMember(Name = "sex")]public string Sex { get; set; }}public partial class _Default : System.Web.UI.Page {string constr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;protected void Page_Load(object sender, p11);szJson = Encoding.UTF8.GetString(stream.ToArray());Response.Write(szJson);}//反序列化//using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(szJson)))//{// DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(People));// Person1 _people = (Person1)serializer.ReadObject(ms);//}}protected void Button1_Click(object sender, EventArgs e){Person p1 = new Person();p1.Id = 1;p1.Name = "dxw";Person p2 = new Person();p2.Id = 2;p2.Name = "wn";ListPerson listperson = new ListPerson();listperson.Add(p1);listperson.Add(p2);JavaScriptSerializer js = new JavaScriptSerializer();//json序列化string s = js.Serialize(listperson);Response.Write(s);//方法二Person1 p11 = new Person1();p11.Id = 1;p11.Name = "hello";p11.Sex = "男";DataContractJsonSerializer json = new DataContractJsonSerializer(p11.GetType());string szJson = "";//序列化using (MemoryStream stream = new MemoryStream()){json.WriteObject(stream,使用DataMember属性标记字段必须使用DataContract标记类 否则DataMember标记不起作用,DataMember:定义序列化属性参数, using System;using System.Collections.Generic;using System.Web.Script.Serialization;using System.Configuration;using System.Runtime.Serialization.Json;using System.Runtime.Serialization;using System.IO;using System.Text;namespace WebApplication1{//方法一:引入System.Web.Script.Serialization命名空间使用 JavaScriptSerializer类实现简单的序列化 [Serializable] public class Person {private int id;/// summary/// id/// /summarypublic int Id{get { return id; }set { id = value; }}private string name;/// summary/// 姓名/// /summarypublic string Name{get { return name; }set { name = value; }} }//方法二:引入 System.Runtime.Serialization.Json命名空间使用 DataContractJsonSerializer类实现序列化 //可以使用IgnoreDataMember:指定该成员不是数据协定的一部分且没有进行序列化。

EventArgs e){Response.Write(constr);}} ,。

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供学习参考!
本文地址为 https://v30.fanwenzhu.com/jiaob/c/11743.shtml

相关文章

风云图片

推荐阅读

返回C#频道首页